import { request } from './request'

class AbbreviationButtonMenu {
    constructor() {
        this.title = '简写'
        this.tag = 'button'
    }

    getValue(editor) {
        return ' hello '
    }

    isActive(editor) {
        return false
    }

    isDisabled(editor) {
        if(editor.getSelectionText()) return false
        return true
    }

    async exec(editor) {
        if (this.isDisabled(editor)) return
        
        const selectionText = editor.getSelectionText().trim()

        const subjectText = `##要求：\n 请帮我将下面这段文字进行简写，要求不改变原本的文意。\n ##输入：\n ${selectionText}`
        
        editor.insertText(selectionText)

        const node = { type: 'paragraph', children: [{ text: '' }] }
        editor.insertNode(node)

        const responseText = await request(subjectText, 2, (text) => {
            editor.insertText(text)
        })

        console.log(responseText)
    }
}

export const Menu = AbbreviationButtonMenu

export default {
    key: 'ai-abbreviation',
    factory() {
      return new AbbreviationButtonMenu()
    }
}